package com.zust.data.serivce;

/**
 * @author senselyan
 * @date 2018/5/11 14:55
 */

import com.zust.data.dal.DlColumnRepository;
import com.zust.data.dal.DlDataRepository;
import com.zust.data.domain.param.DataSaveParam;
import com.zust.data.domain.vo.DataInfoVo;
import com.zust.data.dto.ResponseDto;
import com.zust.data.entity.DlData;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.inject.Inject;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

@Service
public class DataService {

    @Inject
    private DlDataRepository dlDataRepository;
    @Inject
    private DlColumnRepository dlColumnRepository;
    @Inject
    private ColumnService columnService;

    public ResponseDto save(DataSaveParam dataSaveParam) {
        DlData dlData = new DlData();
        if (StringUtils.isNotBlank(dataSaveParam.getId())) {
            dlData = dlDataRepository.findOne(dataSaveParam.getId());
        } else {
            dlData.setCollectNum(0);
            dlData.setLikeNum(0);
            dlData.setReadNum(0);
            dlData.setCreateTime(LocalDateTime.now());
            dlData.setColumnName("name");
            dlData.setValueAName("value1");
            dlData.setValueBName("value2");
            dlData.setValueCName("value3");
            dlData.setValueDName("value4");
            dlData.setValueEName("value5");
        }

        BeanUtils.copyProperties(dataSaveParam, dlData);
        DlData newData = dlDataRepository.save(dlData);

        //当改数据没有字段时候 新建8行空字段数据
        if (0 == dlColumnRepository.countAllByDataIdAndDeletedFlag(newData.getId(), 0)) {
            columnService.addEmptys(newData.getId(), 8);
        }

        return new ResponseDto(true, "操作成功", newData);
    }

    public void changeName(String dataId, Integer type, String value) {
        DlData dlData = dlDataRepository.findOne(dataId);
        if (!ObjectUtils.isEmpty(dlData)) {
            switch (type) {
                case 0: dlData.setColumnName(value);break;
                case 1: dlData.setValueAName(value);break;
                case 2: dlData.setValueBName(value);break;
                case 3: dlData.setValueCName(value);break;
                case 4: dlData.setValueDName(value);break;
                case 5: dlData.setValueEName(value);break;
            }
            dlDataRepository.save(dlData);

        }

    }

    public DataInfoVo info(String dataId) {
        DlData dlData = dlDataRepository.findOne(dataId);
        DataInfoVo dataInfoVo = new DataInfoVo();
        BeanUtils.copyProperties(dlData, dataInfoVo);
        return dataInfoVo;
    }

    public List<DataInfoVo> listByUserIdAndType(String userId, Integer type) {
        List<DlData> dlData = dlDataRepository.findAllByUserIdAndTypeAndDeletedFlag(userId, type, 0);
        List<DataInfoVo> vo = new ArrayList<>();
        for (DlData data : dlData) {
            DataInfoVo dataInfoVo = new DataInfoVo();
            BeanUtils.copyProperties(data, dataInfoVo);
            vo.add(dataInfoVo);
        }
        return vo;
    }

    public void deleted(String dataId) {
        DlData dlData = dlDataRepository.findOne(dataId);
        dlData.setDeletedFlag(1);
        dlDataRepository.save(dlData);
    }

}
